/*
 * graph.cpp
 *
 *  Created on: 10.10.2011
 *      Author: duke
 */

#include <iostream>
#include <vector>
#include <iterator>
#include "graph.h"

using namespace std;

graph::graph() {
}

graph::graph(dynamic_array adj) {
	this->adjacency = adj;
	for (int i = 0; i < this->adjacency.getrows(); i++) {
		for (int j = 0; j <= i; j++) {
			if (adj[i][j] == 1) {
				this->edges.push_back(edge(i, j));
			}
		}
	}
}

/*graph(vector<edge> edges) {
 dynamic_array temp(edges.size(), edges.size());
 this->edges = edges;
 int node1, node2;
 for (vector<edge>::iterator it = edges.begin(); it != edges.end(); ++it) {
 node1 = it->getNode1();
 node2 = it->getNode2();
 temp[node1][node2] = 1;
 }
 this->adjacency = temp;
 }*/

int graph::numOfEdges() {
	return this->edges.size();
}

vector<edge> graph::getAllEdges() {
	return this->edges;
}

bool graph::isTriangleFree() {
	int size = this->adjacency.getcols();
	dynamic_array temp(size, size);
	temp = this->adjacency * this->adjacency * this->adjacency;
	//temp.print();
	for (int i = 0; i < size; i++) {
		if (temp[i][i] != 0)
			return false;
	}
	return true;
}

vector<edge> graph::getEdges(int color) {
	vector<edge> temp;
	for (vector<edge>::iterator it = this->edges.begin(); it
			!= this->edges.end(); ++it) {
		if (it->getColor() == color)
			temp.push_back(*it);
	}
	return temp;
}

void graph::setEdges(vector<edge> edges) {
	this->edges = edges;
	int node1, node2, biggest_node = 0;
	for (vector<edge>::iterator it = edges.begin(); it != edges.end(); it++) {
		node1 = it->getNode1();
		node2 = it->getNode2();
		if (node1 > biggest_node) {
			biggest_node = node1;
		}
		if (node2 > biggest_node) {
			biggest_node = node2;
		}
	}
	dynamic_array temp(biggest_node + 1, biggest_node + 1);
	for (vector<edge>::iterator it = edges.begin(); it != edges.end(); it++) {
		node1 = it->getNode1();
		node2 = it->getNode2();
		temp[node1][node2] = 1;
		temp[node2][node1] = 1;
	}
	this->adjacency = temp;
}

void graph::setEdgeColor(int index, int color) {
	this->edges.at(index).setColor(color);
}

int graph::getEdgeColor(int index) {
	return this->edges.at(index).getColor();
}

void graph::print() {
	cout << "# of edges: " << numOfEdges() << endl;
	cout << "edges:" << endl;
	if (this->edges.size()) {
		for (vector<edge>::iterator it = this->edges.begin(); it
				!= this->edges.end(); it++) {
			it->print();
		}
	} else
		cout << "none..." << endl;
}
